package com.settlement.system.common.enums;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CommonEnumUtil {

    public static<T extends ICommonEnum> T getEnum(Class<T> tClass, String var) {
        for (T t : tClass.getEnumConstants()) {
            if (String.valueOf(t.getNo()).equals(var) || t.getName().equals(var)) {
                return t;
            }
        }
        return null;
    }

    public static<T extends ICommonEnum> T getEnumByNo(Class<T> tClass, String no) {
        for (T t : tClass.getEnumConstants()) {
            if (t.getNo().equals(no)) {
                return t;
            }
        }
        return null;
    }

    public static<T extends ICommonEnum> T getEnumByName(Class<T> tClass, String name) {
        for (T t : tClass.getEnumConstants()) {
            if (t.getName().equals(name)) {
                return t;
            }
        }
        return null;
    }

    public static<T extends ICommonEnum> String getNameByNo(Class<T> tClass, String no) {
        T t = getEnumByNo(tClass, no);
        if(null != t){
            return t.getName();
        }
        return null;
    }

    public static<T extends ICommonEnum> String getNameByNo(String tClassName,  String no) {
        Class<T> aClass = null;
        try {
            aClass = (Class<T>) Class.forName(tClassName);
            T t = getEnumByNo(aClass, no);
            if(null != t){
                return t.getName();
            }
            return null;
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage(),e);
            return null;
        }
    }

    public static<T extends ICommonEnum> String getNoByName(Class<T> tClass, String name) {
        T t = getEnumByName(tClass, name);
        if(null != t){
            return t.getNo();
        }
        return null;
    }

    public static<T extends ICommonEnum> String getNoByName(String tClassName, String name) {
        Class<T> aClass = null;
        try {
            aClass = (Class<T>) Class.forName(tClassName);
            T t = getEnumByName(aClass, name);
            if(null != t){
                return t.getNo();
            }
            return null;
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage(),e);
            return null;
        }
    }

    public static<T extends ICommonEnum> boolean containsName(Class<T> tClass, String name) {
        return null != getEnumByName(tClass,name);
    }
}
